﻿using UnityEngine;
using UnityEngine.UI;
using LitJson;
using Network;
using System;
using Framework;
using System.Collections.Generic;
using Shipwar.SDK;

public class GMConsole : MonoBehaviour {

    public GameObject canvas;
    public InputField inputField;
    public Text textField;

    private void Start()
    {
        NetManager.Instance.RegistMsg((ushort)JsonMsgId.GMConsoleCommand, onGmCommandReturn);
    }

    private void Update()
    {
        if(Input.GetKeyUp("`") || Input.GetKeyUp(KeyCode.F2))
        {
            canvas.SetActive(!canvas.activeSelf);
            if(canvas.activeSelf)
            {
                inputField.ActivateInputField();
            }
            else
            {
                inputField.text = "";
            }
        }

        if(Input.GetKeyUp(KeyCode.Return))
        {
            if(inputField.text.Length > 0)
            {
                string content = inputField.text;
                inputField.text = "";
                inputField.ActivateInputField();
                
                int commandIndex = content.IndexOf(" ");
                string gmCommand = commandIndex >= 0 ? content.Substring(0, commandIndex) : content;
                JsonMsgId gmCommandEnum = 0;
                bool isLocal = false;
                try
                {
                    gmCommandEnum = (JsonMsgId)Enum.Parse(typeof(JsonMsgId), gmCommand);
                }
                catch (ArgumentException e)
                {
                    isLocal = true;
                }
                if(!isLocal)
                {
                    string gmParams = "";
                    if (commandIndex >= 0 && commandIndex + 1 < content.Length)
                        gmParams = "," + content.Substring(commandIndex + 1);
                    textField.text += ("<color=green>" + content + "</color>\n");
                    //send gm
                    ushort gmCommandUshort = (ushort)gmCommandEnum;
                    NetManager.Instance.sendTcpMsg((ushort)JsonMsgId.GMConsoleCommand, gmCommandUshort + gmParams);
                }
                else
                {
                    LocalGMID localGmCommandEnum = 0;
                    try
                    {
                        localGmCommandEnum = (LocalGMID)Enum.Parse(typeof(LocalGMID), gmCommand);
                    }
                    catch
                    {
                        textField.text += ("<color=red>Invalid Command</color>\n");
                        return;
                    }

                    switch(localGmCommandEnum)
                    {
                        case LocalGMID.game:
                            EnterGame();
                            break;
                        case LocalGMID.clear:
                            ClearState();
                            break;
                    }
                }
            }
        }
    }

    void onGmCommandReturn(object msg)
    {
        JsonData json = (JsonData)msg;
        textField.text += ("<color=yellow>" + json.ToJson() + "</color>\n");
        Debug.Log(json.ToJson());
    }


    void EnterGame()
    {
        Mainloop.Instance.ctrl.changeProc(ProcType.PROC_GAME, "0.0.0.0", (ushort)0, (uint)0); 
    }

    void ClearState()
    {
        SDK.GetInstance().QuitLogin();
        PlayerPrefs.DeleteAll();
    }
}
